package com.jin.webvideo.js;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

import javax.script.ScriptException;
import java.io.IOException;

public class ScriptExector {
    private Context context;
    private SourceManager manager;
    public ScriptExector(SourceManager manager, Context.Builder builder){
        context = builder.build();
        this.manager = manager;
    }
    public Value eval(String name) throws ScriptException, IOException {
        Source source = manager.source(name);
        return evalSource(source);
    }
    public Value evalSource(Source source) {
        return context.eval(source);
    }
    public void putMember(String key, Object value){
        context.getBindings(manager.getScriptMimeType()).putMember(key, value);
    }
    public Value parse(String name) throws ScriptException, IOException {
        Source source = manager.source(name);
        return parse(source);
    }
    public Value parse(Source source){
        return context.parse(source);
    }
    public void close(){
        this.close(false);
    }
    public void close(boolean cancelIfExecuting){
        context.close(cancelIfExecuting);
    }
}
